import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { RequestQueryBuilder } from '@nest-crud/request';
import { _HttpClient } from '@delon/theme';
import { FindManyOptionsReq, OP } from '@nest-crud/request-typeorm/find-options';
import { User } from './api';
import { transFind } from '@nest-crud/crud-plus/requset';

@Injectable({
  providedIn: 'root',
})
export class ApiTestService {
  constructor(private http: HttpClient, private http2: _HttpClient) {}
  test() {
    const findOptions: FindManyOptionsReq<User> = {
      select: {
        id: true,
        name: {
          first: true,
          last: true,
        },
        email: true,
        company: {
          name: true,
          projects: {
            name: true,
            isActive: true,
          },
        },
        profile: {
          name: true,
        },
      },
      relations: {
        company: {
          projects: true,
        },
        profile: true,
      },
      where: {
        id: OP.MoreThan(3),
        isActive: true,
        company: { id: 1 },
      },
      order: { profile: { name: 'DESC' } },
      take: 5,
      skip: 0,
      cache: false,
      withDeleted: true,
    };
    const queryString = RequestQueryBuilder.create(transFind(findOptions)).query();
    this.http.get(`/api/companies/1/users?${queryString}`).subscribe();
    this.http.post(`/api/companies/1/users/find`, findOptions).subscribe();
  }
  test2() {
    const queryString = RequestQueryBuilder.create({
      fields: ['id', 'name.first', 'name.last', 'email'],
      join: [
        { field: 'company', select: ['name'] },
        { field: 'company.projects', select: ['name', 'isActive'] },
        { field: 'profile', select: ['name'] },
      ],
      search: {
        id: { $gt: 3 },
        isActive: true,
        'company.id': 1,
      },
      sort: [['profile.name', 'DESC']],
      offset: 0,
      limit: 5,
      resetCache: true,
      includeDeleted: 1,
    }).query();
    console.log('queryString', { params: decodeURI(queryString) });
    this.http.get(`/api/companies/1/users?${queryString}`).subscribe();
  }
}
